#!/usr/bin/python 
#coding=utf-8
import MySQLdb as mdb


class product:
	def __init__(self):
		return;
	def send_query(self):
		self.conn 	= mdb.connect('localhost','root','mgw198100','video_spider');
		self.cursor	= self.conn.cursor(mdb.cursors.DictCursor);
		self.cursor.execute("SET NAMES 'utf8'");
		self.cursor.execute("SELECT * FROM SPIDER_PRODUCT, SPIDER_PRODUCT_BRAND \
					where SPIDER_PRODUCT.product_status='product_new' and SPIDER_PRODUCT.product_brand_id=SPIDER_PRODUCT_BRAND.id LIMIT 1");
		rows 		= self.cursor.fetchall();
		self.cursor.close();
		self.conn.close();

		if len(rows) == 1:
			self.r    	= ("%s %s %s") % (rows[0]['product_brand'],rows[0]['product_sub_brand'],rows[0]['product_name'])
			self.product_id = rows[0]['id'];
			return True
		else:
			return False
	
	def get_product_name(self):
		return self.r;
	def get_product_id(self):
		return self.product_id;
	def handle_product_finish(self):
		self.conn 	= mdb.connect('localhost','root','mgw198100','video_spider');
		self.cursor	= self.conn.cursor(mdb.cursors.DictCursor);
		self.cursor.execute("SET NAMES 'utf8'");
		self.cursor.execute("UPDATE SPIDER_PRODUCT SET product_status='product_handled' WHERE id=%s"%(str(self.product_id)));
		rows 		= self.cursor.fetchall();
		self.cursor.close();
		self.conn.close();


		

if __name__ =="__main__":
	pp = product();
	if pp.send_query():
		print pp.get_product_name();	
		print pp.get_product_id();
		pp.handle_product_finish();
	else:
		print "no result"
